#include "programcheck.h"

//#include <iostream>
//#include <time.h>

ProgramCheck::ProgramCheck()
{
}

int ProgramCheck::patExect(QString programStr,int rowIndex)
{
   return 1;
}

int ProgramCheck::getArmPointIndex(QString programStr)
{
    QString pattern("(.*)\\s+P\\[(\\d+)\\]\\s*(.*)");
    QRegExp rx(pattern);
    int pos = programStr.indexOf(rx);
    if ( pos >= 0 )
    {
        //qDebug() << "point number :" << rx.cap(2);
        return rx.cap(2).toInt();
    }
    else
    {
        return 1;
    }
}

int ProgramCheck::checkPalletCmdList(bool init, QString list,int row)
{
    //time_t start,finish;
    //start = clock();
    if(init)
    {
        //堆栈头
        QString strHead("^\\s*(PALLETIZING-)\\s*(B_|BX_|E_|EX_)(\\d+)\\s*.*$");
        QRegExp rxHead(strHead);
        //堆栈运动
        QString strMotion("^\\s*(J|L)\\s*(PAL_)(\\d+)\\s*\\[\\s*(A_|R_|BTM)(\\d*)\\s*\\]\\s*.*$");
        QRegExp rxMotion(strMotion);
        //辅助点？
        //QString strHand("^\\s*(HAND)\\d+\\s*(OPEN|CLOSE)\\s*$");
        //QRegExp rxHand(strHand);
        //堆栈结束
        QString strEnd("^\\s*(PALLETIZING-END_)(\\d+)\\s*.*$");
        QRegExp rxEnd(strEnd);

        //提取语句
        int pos = -1;
        pos = list.indexOf(rxHead);
        if ( pos >= 0 )
        {
            //qDebug() << "pal_num : " << rxHead.cap(3) << " ; type : 0 ; row : "<<row;

            int number = rxHead.cap(3).toInt();//== size

            if(number > 0)
            {
                QVector <int > tmp;
                tmp.resize(5);//head a btm r end
                tmp[0] = -1;
                tmp[1] = -1;
                tmp[2] = -1;
                tmp[3] = -1;
                tmp[4] = -1;

                for(int i = 0;i<number;i++)
                {
                    if(palletListRow.size() < number)
                    {
                        palletListRow.append(tmp);
                    }
                }

                palletListRow[number-1][PAL_CHECK_HEAD] = row;
            }
        }

        pos = list.indexOf(rxMotion);
        if ( pos >= 0 )
        {
            int tmpType;
            if("A_" == rxMotion.cap(4))
            {
                tmpType = PAL_CHECK_A;
            }
            else if("R_" == rxMotion.cap(4))
            {
                tmpType = PAL_CHECK_R;
            }
            else if("BTM" == rxMotion.cap(4))
            {
                tmpType = PAL_CHECK_BTM;
            }
            //qDebug() << "pal_num : " << rxMotion.cap(3) << " ; type : "<< tmpType<<" ; row : "<<row;

            int number = rxMotion.cap(3).toInt();
            if(number > 0 && number-1 < palletListRow.size())
            {
                palletListRow[number-1][tmpType] = row;
            }
        }

//        pos = list.indexOf(rxHand);
//        if ( pos >= 0 )
//        {}

        pos = list.indexOf(rxEnd);
        if ( pos >= 0 )
        {
            //qDebug() << "pal_num : " << rxEnd.cap(2) << " ; type : 4 ; row : "<<row;

            int number = rxEnd.cap(2).toInt();
            if(number > 0 && number-1 < palletListRow.size())
            {
                palletListRow[number-1][PAL_CHECK_END] = row;
            }
        }
    }
    else
    {
        //初始化完成:开始判别合法性
        //1:判断 单独码垛块的合法性
        for(int pal = 0;pal < palletListRow.size();pal++)
        {
            if(5 == palletListRow[pal].size())
            {
                //1.1 判断缺漏
                if(-1 == palletListRow[pal][PAL_CHECK_HEAD] &&
                        -1 == palletListRow[pal][PAL_CHECK_A] &&
                        -1 == palletListRow[pal][PAL_CHECK_BTM] &&
                        -1 == palletListRow[pal][PAL_CHECK_R] &&
                        -1 == palletListRow[pal][PAL_CHECK_END])
                {
                    continue;//跳出循环,判断下一个;因为该码垛不存在,全-1默认值
                }
                else
                {
                    if(-1 == palletListRow[pal][PAL_CHECK_HEAD] ||
                            -1 == palletListRow[pal][PAL_CHECK_A] ||
                            -1 == palletListRow[pal][PAL_CHECK_BTM] ||
                            -1 == palletListRow[pal][PAL_CHECK_R] ||
                            -1 == palletListRow[pal][PAL_CHECK_END])
                    {
                        palletListRow.clear();
                        return pal+1;//该码垛有缺漏;返回异常的码垛编号
                    }
                }

                //1.2 判断顺序
//                qDebug()<<palletListRow[pal][PAL_CHECK_HEAD]<<
//                          palletListRow[pal][PAL_CHECK_A]<<
//                          palletListRow[pal][PAL_CHECK_BTM]<<
//                          palletListRow[pal][PAL_CHECK_R]<<
//                          palletListRow[pal][PAL_CHECK_END];
                if(palletListRow[pal][PAL_CHECK_HEAD] > palletListRow[pal][PAL_CHECK_A]
                        || palletListRow[pal][PAL_CHECK_HEAD] > palletListRow[pal][PAL_CHECK_BTM]
                        || palletListRow[pal][PAL_CHECK_HEAD] > palletListRow[pal][PAL_CHECK_R]
                        || palletListRow[pal][PAL_CHECK_HEAD] > palletListRow[pal][PAL_CHECK_END])
                {
                    palletListRow.clear();
                    return pal+1;//返回异常的码垛编号
                }
                if(palletListRow[pal][PAL_CHECK_A] > palletListRow[pal][PAL_CHECK_BTM]
                        || palletListRow[pal][PAL_CHECK_A] > palletListRow[pal][PAL_CHECK_R]
                        || palletListRow[pal][PAL_CHECK_A] > palletListRow[pal][PAL_CHECK_END])
                {
                    palletListRow.clear();
                    return pal+1;//返回异常的码垛编号
                }
                if(palletListRow[pal][PAL_CHECK_BTM] > palletListRow[pal][PAL_CHECK_R]
                        || palletListRow[pal][PAL_CHECK_BTM] > palletListRow[pal][PAL_CHECK_END])
                {
                    palletListRow.clear();
                    return pal+1;//返回异常的码垛编号
                }
                if(palletListRow[pal][PAL_CHECK_R] > palletListRow[pal][PAL_CHECK_END])
                {
                    palletListRow.clear();
                    return pal+1;//返回异常的码垛编号
                }
            }
        }

        //2:判断码垛之间的合法性
        for(int pal = 0;pal < palletListRow.size();pal++)
        {
            for(int i = pal;i < palletListRow.size();i++)
            {
                if( -1 == palletListRow[i][PAL_CHECK_HEAD])
                {
                    continue;//此码垛数据为空置,跳过不判断
                }

                //判断 有无部分交集 ; 两两互换情况检验
                if(palletListRow[pal][PAL_CHECK_HEAD] < palletListRow[i][PAL_CHECK_END] &&
                        palletListRow[pal][PAL_CHECK_HEAD] > palletListRow[i][PAL_CHECK_HEAD])
                {
                    //pal头 在i的内部;但pal尾 在i的尾的下方
                    if(palletListRow[pal][PAL_CHECK_END] > palletListRow[i][PAL_CHECK_END] )
                    {
                        palletListRow.clear();
                        return pal+1;//返回异常的码垛编号
                    }
                }
                if(palletListRow[pal][PAL_CHECK_END] < palletListRow[i][PAL_CHECK_END] &&
                        palletListRow[pal][PAL_CHECK_END] > palletListRow[i][PAL_CHECK_HEAD])
                {
                    //pal尾 在i的内部;但pal头 在i的头的上方
                    if(palletListRow[pal][PAL_CHECK_HEAD] < palletListRow[i][PAL_CHECK_HEAD] )
                    {
                        palletListRow.clear();
                        return pal+1;//返回异常的码垛编号
                    }
                }
            }
        }

        //finish = clock();
        //qDebug()<<"time :"<<double(finish - start) / 1000;

        //3:结束 返回
        palletListRow.clear();
        return 0;//表示没有编号,表示正常
    }

    return false;
}
